package com.huluobo.a2208parkdemo.sign

import android.os.Bundle
import android.util.Log
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.AMap
import com.amap.api.maps.model.MyLocationStyle
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208parkdemo.databinding.ActivitySignBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody


@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding, SignViewModel>() {
    private lateinit var map: AMap
    private lateinit var myLocationStyle: MyLocationStyle

    //    //声明AMapLocationClient类对象
    lateinit var mLocationClient: AMapLocationClient
    lateinit var mLocationOption: AMapLocationClientOption

    //声明定位回调监听器
    val mLocationListener = AMapLocationListener {
        if (it.errorCode == 0) {
            //可在其中解析amapLocation获取相应内容。
            Log.i("TagA", "定位成功:${it.address}")
        } else {
            //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
            Log.e(
                "TagA", "定位黑白, 错误码:"
                        + it.errorCode + ", errInfo:"
                        + it.locationDetail
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.signMap.onCreate(savedInstanceState)
        map = binding.signMap.map
        myLocationStyle = MyLocationStyle()
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        map.myLocationStyle = myLocationStyle;//设置定位蓝点的Style
//aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        map.isMyLocationEnabled = true;// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        binding.signMap.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        binding.signMap.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        binding.signMap.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.signMap.onSaveInstanceState(outState)
    }

    override fun initData() {
        vm.signStartSuccess.observe(this) {
            ToastUtils.showLong("签到成功")
            binding.signStartBtn.text = "签退"
            SPUtils.getInstance().put(Const.PARAM_IS_SIGN, true)
        }

        vm.signEndSuccess.observe(this) {
            ToastUtils.showLong("签退成功")
            binding.signStartBtn.text = "签到"
            SPUtils.getInstance().put(Const.PARAM_IS_SIGN, false)
        }
    }

    override fun initView() {
        binding.signHeader.headerBack.setOnClickListener { finish() }
        binding.signHeader.headerTitle.text = "签到/签退"
        //初始化定位
        mLocationClient = AMapLocationClient(applicationContext)
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener)

        mLocationOption = AMapLocationClientOption()
        //设置返回定位地址
        mLocationOption.isNeedAddress = true;
        //设置定位模式
        mLocationOption.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.SignIn
        mLocationClient.setLocationOption(mLocationOption);
        //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
        mLocationClient.stopLocation();
        mLocationClient.startLocation();

        if (SPUtils.getInstance().getBoolean(Const.PARAM_IS_SIGN)) {
            binding.signStartBtn.text = "签退"
        } else {
            binding.signStartBtn.text = "签到"
        }

        binding.signStartBtn.setOnClickListener {
            if (SPUtils.getInstance().getBoolean(Const.PARAM_IS_SIGN)) {
                //签退
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_SIGN_END_ADDRESS] = "天津市"
                map[Const.PARAM_SIGN_END_TIME] = System.currentTimeMillis() / 1000
                map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_UID)

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.signEnd(body)
            } else {
                //签到
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_SIGN_START_ADDRESS] = "天津市"
                map[Const.PARAM_SIGN_START_TIME] = System.currentTimeMillis() / 1000
                map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_UID)

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.signStart(body)
            }
        }
    }

    fun dingwei() {

    }
}